using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WheelSkidSoft : MonoBehaviour
{
	public Rigidbody rb;
	public Skidmarks skidmarks;
	private SphereCollider sphereCollider;
	public TireSmoke smoke;
	public AudioSource skidSound;
	public AudioSource tireImpact;


	private Rigidbody wheel;

	private const float MaxSkidIntensity = 20.0f;
	private int lastSkid = -1;
	private float lastFixedUpdateTime;
	private SoftWheelCollider softWheelCollider;


	protected void Awake()
	{
        softWheelCollider = GetComponent<SoftWheelCollider>();
		lastFixedUpdateTime = Time.time;
		wheel = GetComponent<Rigidbody>();
		sphereCollider = GetComponent<SphereCollider>();
		skidSound.mute = true;
		skidmarks = FindFirstObjectByType<Skidmarks>();
	}

	protected void FixedUpdate()
	{
		lastFixedUpdateTime = Time.time;

        // exit
        if (softWheelCollider.ContactColliders.Length > 0)
        {
			AddSkid();

		}
        else
        {
			skidSound.mute = true;
			if (smoke)
			{
				smoke.stopSmoke();
			}
		}
	}

	public void AddSkid()
    {
		float wheelAngularVelocity = Mathf.Abs(transform.InverseTransformDirection(wheel.angularVelocity).x);
		float wheelForwardVel = Mathf.Abs(Vector3.Dot(transform.InverseTransformDirection(wheel.linearVelocity), rb.transform.forward));
		float radius = sphereCollider.bounds.extents.y;

		float skidTotal = Mathf.Abs(wheelAngularVelocity * radius - transform.InverseTransformDirection(wheel.linearVelocity).magnitude);


		if (wheelAngularVelocity * sphereCollider.bounds.extents.x > wheelForwardVel + 1)
		{
			skidSound.mute = false;
			float intensity = Mathf.Clamp01(skidTotal / MaxSkidIntensity);
			skidSound.volume = intensity / 3;

			Vector3 skidPoint = softWheelCollider.contactPoint + (rb.linearVelocity * (Time.time - lastFixedUpdateTime));
			if (!softWheelCollider.ContactColliders[0].GetComponent<Rigidbody>())
			{
				lastSkid = skidmarks.AddSkidMark(skidPoint, softWheelCollider.NormalDir, intensity, lastSkid);
			}

			if (smoke && intensity > 0.5f)
			{
				smoke.playSmoke();
			}
			else if (smoke)
			{
				smoke.stopSmoke();
			}
		}
		else if (wheelAngularVelocity * sphereCollider.bounds.extents.x < wheelForwardVel - 1)
		{
			skidSound.mute = false;
			float intensity = Mathf.Clamp01(skidTotal / MaxSkidIntensity);
			skidSound.volume = intensity / 3;

			Vector3 skidPoint = softWheelCollider.contactPoint + (rb.linearVelocity * (Time.time - lastFixedUpdateTime));

			if (!softWheelCollider.ContactColliders[0].GetComponent<Rigidbody>())
			{
				lastSkid = skidmarks.AddSkidMark(skidPoint, softWheelCollider.NormalDir, intensity, lastSkid);
			}

			if (smoke && intensity > 0.5f)
			{
				smoke.playSmoke();
			}
			else if (smoke)
			{
				smoke.stopSmoke();
			}

		}
		else
		{
			skidSound.mute = true;
			lastSkid = -1;
			if (smoke)
			{
				smoke.stopSmoke();
			}
		}
	}

    /*private void OnTriggerStay(Collider other)
	{
		float wheelAngularVelocity = Mathf.Abs(transform.InverseTransformDirection(wheel.angularVelocity).x);
		float wheelForwardVel = Mathf.Abs(Vector3.Dot(transform.InverseTransformDirection(wheel.velocity), rb.transform.forward));
		float radius = sphereCollider.bounds.extents.y;

		float skidTotal = Mathf.Abs(wheelAngularVelocity * radius - transform.InverseTransformDirection(wheel.velocity).magnitude);


		if (wheelAngularVelocity * sphereCollider.bounds.extents.x > wheelForwardVel + 1)
		{
			skidSound.mute = false;
			float intensity = Mathf.Clamp01(skidTotal / MaxSkidIntensity);
			skidSound.volume = intensity / 3;

			Vector3 skidPoint = fakeSoftSphere.contactPoint + (rb.velocity * (Time.time - lastFixedUpdateTime));
			if (!other.GetComponent<Rigidbody>())
			{
				lastSkid = skidmarks.AddSkidMark(skidPoint, fakeSoftSphere.NormalDir, intensity, lastSkid);
			}

			if (smoke && intensity > 0.5f)
			{
				smoke.playSmoke();
			}
			else if (smoke)
			{
				smoke.stopSmoke();
			}
		}
		else if (wheelAngularVelocity * sphereCollider.bounds.extents.x < wheelForwardVel - 1)
		{
			skidSound.mute = false;
			float intensity = Mathf.Clamp01(skidTotal / MaxSkidIntensity);
			skidSound.volume = intensity / 3;

			Vector3 skidPoint = fakeSoftSphere.contactPoint + (rb.velocity * (Time.time - lastFixedUpdateTime));

			if (!other.GetComponent<Rigidbody>())
			{
				lastSkid = skidmarks.AddSkidMark(skidPoint, fakeSoftSphere.NormalDir, intensity, lastSkid);
			}

			if (smoke && intensity > 0.5f)
			{
				smoke.playSmoke();
			}
			else if (smoke)
			{
				smoke.stopSmoke();
			}

		}
		else
		{
			skidSound.mute = true;
			lastSkid = -1;
			if (smoke)
			{
				smoke.stopSmoke();
			}
		}

	}*/

    private void OnTriggerEnter(Collider other)
    {
		//tireImpact.Play();
	}

}
